SharePoint REST API to retrieve more than 5000 items.

If you have ever worked on SharePoint REST API, you may have notice that by default it returns only 100 records.

The OData provider for the SharePoint REST API is configured to limit the number of items in a response to 100. It protects against developers accidentally making a request that returns large result sets.

Use “$top” to retrieve more than 100 items but up to 5000 only:

Below is the example on how to use $top :

.webAbsoluteUrl + "/_api/web/lists/getbytitle('myList')/items?$top=1000


Note: 

  • $top — It is used to control the page size. We can use $top to get more than 100 items but there is also limitation that you cannot get more than 5000 items using $top.

But what if I want to retrieve more than 5000 items ?

The solution is to use Recursive call to retrieve more than 5000 items:

See below example :

 var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('DocumentList')/items?$top=1000";
    var response = response || [];  // this variable is used for storing list items
    function GetListItems(){
        $.ajax({
            url: url,  
            method: "GET",  
            headers: {  
                "Accept": "application/json; odata=verbose"  
            },
            success: function(data){
                response = response.concat(data.d.results);
                if (data.d.__next) {
                    url = data.d.__next;
                    GetListItems();
                }
            },
            error: function(error){
                   // error handler code goes here
            }
        });
    }

Here we are doing recursive call to the GetListItems() function. This will fetch data in the bunch of 1000 items and concatenate it to response variable.

Here data.d.__next contains the url to get the next set of items. In case we don’t have more items left to fetch, it will not call GetListItems() function.

12 thoughts on “SharePoint REST API to retrieve more than 5000 items.

  1. Pingback: SharePoint REST API to retrieve more than 5000 items. — SharePoint | southbuzz

  2. Pingback: SharePoint REST API to retrieve more than 5000 items. – Bharath Radhekrishna

Leave a comment